﻿/// <reference path="/Scripts/jquery-1.5.1.min.js">
function jsPlayListStorage() {
}

jsPlayListStorage.addPlayList = function (id, name, artist, location) {
    if (localStorage.playList == undefined) {
        localStorage.playList = "{id:" + id + ",name:'" + escape(name) + "',artist:'" + escape(artist) + "',location:'" + location + "',guid:'" + guidGenerator() + "'},"
    }
    else {
        localStorage.playList += "{id:" + id + ",name:'" + escape(name) + "',artist:'" + escape(artist) + "',location:'" + location + "',guid:'" + guidGenerator() + "'},"
    }
}

jsPlayListStorage.addPlayingList = function (id, name, artist, location) {
    if (localStorage.playingList == undefined) {
        localStorage.playingList = "{id:" + id + ",name:'" + escape(name) + "',artist:'" + escape(artist) + "',location:'" + location + "',guid:'" + guidGenerator() + "'},"
    }
    else {
        localStorage.playingList += "{id:" + id + ",name:'" + escape(name) + "',artist:'" + escape(artist) + "',location:'" + location + "',guid:'" + guidGenerator() + "'},"
    }
}

jsPlayListStorage.getPlayList = function (listID) {

    var strPlayList = localStorage.playList;
    if (strPlayList != undefined) {
        var json = eval("[" + strPlayList + "]");
        for (var i = 0; i < json.length; i++) {          
            if ($("[data-val='" + $.trim(json[i].guid) + "']").length == 0) {
                $("#" + listID).append("<li><div class='div_PlayListItem' data-id=" + json[i].id + " data-val='" + $.trim(json[i].guid) + "'  data-location='" + unescape(json[i].location) + "'><div class='divPlay'></div><div style='float:left; width:550px; cursor:pointer;'>" + unescape(json[i].name) + "</div><div style='float:left;width:300px;'>" + unescape(json[i].artist) + "</div><div style='cursor:pointer;'></div></div></li>");
            }
        }
    }

}

jsPlayListStorage.getPlayingList = function (listID) {
    var strPlayList = localStorage.playingList;
    if (strPlayList != undefined) {
        var json = eval("[" + strPlayList + "]");
        for (var i = 0; i < json.length; i++) {            
            if ($("[data-val='" + $.trim(json[i].guid) + "']").length == 0) {
                $("<li><div class='div_PlayListItem' data-id=" + json[i].id + " data-val='" + $.trim(json[i].guid) + "'  data-location='" + unescape(json[i].location) + "'><div class='divPlay'></div><div style='float:left; width:550px;cursor:pointer;'>" + unescape(json[i].name) + "</div><div style='float:left; width:300px;'>" + unescape(json[i].artist) + "</div><div  style='cursor:pointer;'></div></div></li>").insertAfter("#" + listID);
            }
        }
    }
}

jsPlayListStorage.clearPlayList = function () {
    localStorage.playList = "";
    localStorage.playingList = "";
    $(".div_PlayListItem").remove();
}

jsPlayListStorage.deleteSingleSong = function (guid) {
    var index1 = -1;
    var index2 = -1;
    var str1 = "";
    var str2 = "";

   
    if (localStorage.playList != undefined) {
        index1 = localStorage.playList.indexOf(guid);
        if (index1 >= 0) {
            index2 = localStorage.playList.lastIndexOf('{', index1);
            str1 = localStorage.playList.substring(0, index2);
            str2 = localStorage.playList.substring(index1 + guid.length + 3);
            localStorage.playList = str1 + str2;
        }
        else {
            if (localStorage.playingList != undefined) {
                index1 = localStorage.playingList.indexOf(guid);
                if (index1 >= 0) {
                    index2 = localStorage.playingList.lastIndexOf('{', index1);
                    str1 = localStorage.playingList.substring(0, index2);
                    str2 = localStorage.playingList.substring(index1 + guid.length + 3);
                    localStorage.playingList = str1 + str2;
                }
            }
        }       
    }
    else {
        if (localStorage.playingList != undefined) {
            index1 = localStorage.playingList.indexOf(guid);
            if (index1 >= 0) {
                index2 = localStorage.playingList.lastIndexOf('{', index1);
                str1 = localStorage.playingList.substring(0, index2);
                str2 = localStorage.playingList.substring(index1 + guid.length + 3);
                localStorage.playingList = str1 + str2;
            }
        }
    }

}

function guidGenerator() {
    var S4 = function () {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}